﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Billing.Windows
{
    public partial class BulkOrderBoxDetails : Window
    {
        BOBDetails details;
        Color cursorEnterColor = Colors.Violet;
        Color cursorLeaveColor = Color.FromArgb(Convert.ToByte("FF", 16), Convert.ToByte("65", 16), Convert.ToByte("65", 16), Convert.ToByte("65", 16));
        //double Total;

        public BulkOrderBoxDetails(BOBDetails dts)
        {
            InitializeComponent();
            details = dts;

            int index = 0;

            for (int cnt = 0; cnt < 20; cnt++)
            {
                cmbDate.Items.Add(new ListBoxItem { Tag = DateTime.Now.AddDays(cnt), Content = DateTime.Now.AddDays(cnt).DayOfWeek.ToString() + "-" + DateTime.Now.AddDays(cnt).ToString("dd-MMM-yyyy") });

                if (DateTime.Now.AddDays(cnt).Day == details.DeliveryDate.Day && DateTime.Now.AddDays(cnt).Month == details.DeliveryDate.Month && DateTime.Now.AddDays(cnt).Year == details.DeliveryDate.Year)
                {
                    index = cnt;
                }
            }

            cmbDate.SelectedIndex = index;
            txtPhone.Text = details.PhoneNumber;
            txtCustomer.Text = details.CustomerName;
            description.Text = details.AdditionalDetails;
            txtDeliveryPlace.Text = details.DeliveryPlace;

            if (details.DeliveryTime != string.Empty)
            {
                int cntr = -1;
                foreach (ListBoxItem item in lstTime.Items)
                {
                    cntr++;
                    if (item.Content.ToString() == details.DeliveryTime)
                    {
                        lstTime.SelectedIndex = cntr;
                        lstTime.ScrollIntoView(lstTime.SelectedItem);
                        break;
                    }
                }
            }
            txtCustomer.Focus();
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Right)
                {
                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
                    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                    if (elementWithFocus != null)
                        elementWithFocus.MoveFocus(request);
                }
                else if (e.Key == Key.Left)
                {
                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Previous);
                    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                    if (elementWithFocus != null)
                        elementWithFocus.MoveFocus(request);
                }

                if (e.Key == Key.Escape || e.Key == Key.Enter)
                {
                    if (txtPhone.Text == string.Empty)
                    {
                        dphone.Foreground = new SolidColorBrush(Colors.Red);
                        return;
                    }

                    dphone.Foreground = new SolidColorBrush(Colors.White);

                    details.PhoneNumber = txtPhone.Text;
                    details.DeliveryDate = DateTime.Parse((cmbDate.SelectedItem as ListBoxItem).Tag.ToString());
                    details.AdditionalDetails = description.Text;
                    details.CustomerName = txtCustomer.Text;
                    int time = 0;
                    if (lstTime.SelectedItem == null)
                    {
                        time = 7;
                        details.DeliveryTime = "7 AM";
                    }
                    else
                    {
                        time = int.Parse((lstTime.SelectedItem as ListBoxItem).Tag.ToString());
                        details.DeliveryTime = (lstTime.SelectedItem as ListBoxItem).Content.ToString();
                    }
                    details.DeliveryPlace = txtDeliveryPlace.Text;

                    if (DateTime.Now.AddHours(2) > details.DeliveryDate.Date.AddHours(time))
                    {
                        brdrDate.Background = new SolidColorBrush(Colors.Red);
                        dd.Foreground = new SolidColorBrush(Colors.Red);
                    }
                    else
                    {
                        brdrDate.Background = new SolidColorBrush(cursorLeaveColor);
                        dd.Foreground = new SolidColorBrush(Colors.White);
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void txt_GotFocus(object sender, RoutedEventArgs e)
        {
            ((sender as TextBox).Parent as Border).Background = new SolidColorBrush(cursorEnterColor);
        }

        private void txt_LostFocus(object sender, RoutedEventArgs e)
        {
            ((sender as TextBox).Parent as Border).Background = new SolidColorBrush(cursorLeaveColor);
        }

        private void dt_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            ((sender as ComboBox).Parent as Border).Background = new SolidColorBrush(cursorEnterColor);
        }

        private void dt_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            ((sender as ComboBox).Parent as Border).Background = new SolidColorBrush(cursorLeaveColor);
        }

        private void cmbDate_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Right || e.Key == Key.Left)
                e.Handled = true;
            else
                e.Handled = false;
        }
    }
}
